import { fakeRegister, checkName, checkMobile, checkCaptcha, sendCaptcha } from '../services/api';
import { setAuthority } from '../utils/authority';
import { reloadAuthorized } from '../utils/Authorized';

export default {
  namespace: 'register',

  state: {
    status: undefined,
  },

  effects: {
    *submit({ payload }, { call, put }) {
      debugger;
      const response = yield call(fakeRegister, payload);
      yield put({
        type: 'registerHandle',
        payload: response,
      });
    },
    *checkName({ payload }, { call, put }) {
      const response = yield call(checkName, payload);
      yield put({
        type: 'show',
        payload: response,
      });
    },
    *checkMobile({ payload }, { call, put }) {
      const response = yield call(checkMobile, payload);
      yield put({
        type: 'show',
        payload: response,
      });
    },
    *checkCaptcha({ payload }, { call, put }) {
      const response = yield call(checkCaptcha, payload);
      yield put({
        type: 'show',
        payload: response,
      });
    },
    *sendCaptcha({ payload }, { call, put }) {
      const response = yield call(sendCaptcha, payload);
      yield put({
        type: 'show',
        payload: response,
      });
    },
  },

  reducers: {
    registerHandle(state, { payload }) {
      setAuthority('user');
      reloadAuthorized();
      return {
        ...state,
        status: payload.status,
      };
    },
    show(state, { payload }) {
      return {
        ...state,
        type: payload.type,
        code: payload.code,
      };
    },
  },
};
